//
//  MedQuestTests.m
//  MedQuestTests
//
//  Created by Justin Grant on 10/27/12.
//  Copyright (c) 2012 TeamIota. All rights reserved.
//

#import "MedQuestTests.h"
#import "CATQuestionnaire.h"
#import "MedQuestDBObject.h"
#import "Question.h"

@implementation MedQuestTests

- (void)setUp
{
    [super setUp];
    
    // Set-up code here.
}

- (void)tearDown
{
    // Tear-down code here.
    
    [super tearDown];
}

- (void)testQuestionnaire 
{
    CATQuestionnaire* q = [CATQuestionnaire createQuestionnaire:@"CAT" withId:[[NSNumber alloc] initWithInt:0]];
    NSMutableArray* questions = [[NSMutableArray alloc] init];
    STAssertNotNil(q, @"Allocation Test.");
    
    NSArray *detailArray = [[NSArray alloc]initWithObjects:@"I never cough",@"I cough all the time", nil];
    [questions addObject:[Question createQuestion:@"Cough" withDetails:detailArray withId:[[NSNumber alloc] initWithInt:0]]];
    [q addQuestion:[questions objectAtIndex:(questions.count - 1)]];
    
    detailArray = [[NSArray alloc]initWithObjects:@"I have no phlegm (mucus) in my chest at all",@"My chest is completely full of phlegm (mucus)", nil];
    [questions addObject:[Question createQuestion:@"Phlegm" withDetails:detailArray withId:[[NSNumber alloc] initWithInt:1]]];
    [q addQuestion:[questions objectAtIndex:(questions.count - 1)]];
    
    detailArray = [[NSArray alloc]initWithObjects:@"My chest does not feel tight at all",@"My chest feels very tight", nil];
    [questions addObject:[Question createQuestion:@"Tight chest" withDetails:detailArray withId:[[NSNumber alloc] initWithInt:2]]];
    [q addQuestion:[questions objectAtIndex:(questions.count - 1)]];
    
    detailArray = [[NSArray alloc]initWithObjects:@"When I walk up a hill or one flight of stairs I am not breathless",@"When I walk up a hill or one flight to stairs I am very breathless", nil];
    [questions addObject:[Question createQuestion:@"Breathless" withDetails:detailArray withId:[[NSNumber alloc] initWithInt:3]]];
    [q addQuestion:[questions objectAtIndex:(questions.count - 1)]];
    
    detailArray = [[NSArray alloc]initWithObjects:@"I am not limited doing any activities at home",@"I am very limited doing activities at home", nil];
    [questions addObject:[Question createQuestion:@"Limited activities" withDetails:detailArray withId:[[NSNumber alloc] initWithInt:4]]];
    [q addQuestion:[questions objectAtIndex:(questions.count - 1)]];
    
    detailArray = [[NSArray alloc]initWithObjects:@"I am confident leaving my home despite my lung condition",@"I am not at all confident leaving my home because of my lung condition", nil];
    [questions addObject:[Question createQuestion:@"Home bound" withDetails:detailArray withId:[[NSNumber alloc] initWithInt:5]]];
    [q addQuestion:[questions objectAtIndex:(questions.count - 1)]];
    
    detailArray = [[NSArray alloc]initWithObjects:@"I sleep soundly",@"I don't sleep soundly because of my lung condition", nil];
    [questions addObject:[Question createQuestion:@"Sleep" withDetails:detailArray withId:[[NSNumber alloc] initWithInt:6]]];
    [q addQuestion:[questions objectAtIndex:(questions.count - 1)]];
    
    detailArray = [[NSArray alloc]initWithObjects:@"I have lots of energy",@"I have no energy at all", nil];
    [questions addObject:[Question createQuestion:@"Energy" withDetails:detailArray withId:[[NSNumber alloc] initWithInt:7]]];
    [q addQuestion:[questions objectAtIndex:(questions.count - 1)]];
    
    NSArray* questionsOut = [q getQuestions];
    STAssertNotNil(questions, @"Question return");
    
    for(int i = 0; i < questionsOut.count; i++)
    {
        STAssertEqualObjects(questions[i], questionsOut[i], @"Testing input questions are equal: %d", i);
    }
    

    for(int i = 0; i <= 7; i++)
    {
        STAssertEquals([q setAnswer:[NSNumber numberWithInt:0] forQuestion:[NSNumber numberWithInt:i]], YES, @"Testing providing answers %d", i);
    }

}

- (void) testDatabase
{
    
    NSString* newPatientName = @"Jis Ben";
    int patientID = 0;
    int questID = 0;
    
    MedQuestDBObject *db = [[MedQuestDBObject alloc] init];
    STAssertNotNil(db, @"Allocation Test.");
    
    
    // Query the database for new, empty questionnaires
    NSMutableArray *questionnaires = [db readEmptyQuestionnairesFromDatabase];
    STAssertNotNil(questionnaires, @"Making sure the DB has the initial set of questionnaires");

    // Returns an array of all the patients stored in the database
    for (Patient *p in [db readPatientsFromDatabase])
    {
        STAssertNotNil(p, @"Read all Patients Test");
    }
    
    // Returns an array of all patients in the database with the given name
    for (Patient *p in [db readPatientsFromDatabaseWithName:newPatientName])
    {
        STAssertNotNil(p, @"Existing Patient Read Test");
        STAssertTrue([p.name isEqualToString:newPatientName], @"Checking Patient Name retrieval");
        STAssertTrue([p.email isEqualToString:@"email@email.com"], @"Checking Patient email retrieval");
        STAssertTrue([p.address isEqualToString:@"82 My Home Rd, Campbell, CA 95008"], @"Checking Patient Address retrieval");
        STAssertTrue([p.phoneNumber isEqualToString:@"1800DEDBEEF"], @"Checking Patient Phone Number retrieval");
        // If loop runs again, this should fail... There should only be 1 "Jis Ben"
    }
    
    patientID = [db addPatientToDatabaseWithName:@"Wonderful Tester" DOB:@"1976-09-03" Email:@"wonder@ful.com" Address:@"hello world" PhoneNumber:@"432123444"];
    STAssertTrue(patientID >= 0, @"DB Patient Insert Test");
    
    for (Patient *p in [db readPatientsFromDatabaseWithName:@"Wonderful Tester Man"])
    {
        STAssertNotNil(p, @"Patient Insert Test");
        STAssertEquals(p.ID, patientID, @"Patient ID ?== return value Test");
        STAssertTrue([p.name isEqualToString:@"Wonderful Tester Man"], @"DB Insert Test: Name");
        STAssertTrue([p.email isEqualToString:@"wonder@ful.com"], @"DB Insert Test: Email");
        STAssertTrue([p.address isEqualToString:@""], @"DB Insert Test & NULL Test: Address");
        STAssertTrue([p.phoneNumber isEqualToString:@""], @"DB Insert Test: Phone Number");
    }
    
   // NSString* inputQuestions[] = { @"Question1", @"Question2", @"Question3" };
    CATQuestionnaire *q = [[CATQuestionnaire alloc] init];

    NSArray* questions = [q getQuestions];
    for(int i = 0; i < questions.count; i++)
    {
        STAssertEquals([q setAnswer:[NSNumber numberWithInt:i] forQuestion:questions[i]], YES, @"Testing good question %d", i);
    }
    //STAssertEquals([q setAnswer:[NSNumber numberWithInt:4] forQuestion:questions[0]], YES, @"Testing maximum output");
    
    questID = [db addToDatabaseForPatientWithID:patientID aQuestionnaire:q];
    STAssertTrue(questID >= 0, @"DB Questionnaire Insert Test");

    for (Questionnaire *quest in [db readAnsweredQuestionnairesFromDatabaseForPatientWithID:patientID])
    {
        STAssertNotNil(quest, @"Questionnaire Insert Test");
//        STAssertEquals([quest getAnswer:inputQuestions[0]].intValue, [q getAnswer:inputQuestions[0]].intValue, @"Testing Questionnaire Insert and Read");
    }
    
}

@end
